package com.stylefeng.guns.modular.front.controller;

import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.common.persistence.model.Division;
import com.stylefeng.guns.common.persistence.model.GloryUser;
import com.stylefeng.guns.common.persistence.model.Setting;
import com.stylefeng.guns.common.persistence.model.ShareRecord;
import com.stylefeng.guns.core.base.controller.FrontBaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.modular.flagglory.service.IDivisionService;
import com.stylefeng.guns.modular.flagglory.service.IGloryUserService;
import com.stylefeng.guns.modular.flagglory.service.ISettingService;
import com.stylefeng.guns.modular.flagglory.service.IShareRecordService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/2/7
 * Time: 14:44
 */
@RestController
@RequestMapping("/front/division")
public class FrontShareController extends FrontBaseController {
    @Autowired
    private IShareRecordService shareRecordService;
    @Autowired
    private ISettingService settingService;
    @Autowired
    private IGloryUserService userService;

    @RequestMapping(value = "/addShare", method = RequestMethod.GET)
    @ApiOperation("添加分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", paramType = "query", defaultValue = "9"),
            @ApiImplicitParam(name = "message", value = "分享信息", dataType = "String", paramType = "query", defaultValue = "asdasdasd")
    })
    public Object addShare(Integer userId, String message) {
        ShareRecord shareRecord = new ShareRecord();
        shareRecord.setUserid(userId);
        shareRecord.setMessage(message);
        shareRecord.setCreatetime(new Date());
        shareRecordService.insert(shareRecord);
        Setting setting = settingService.getSetting("share_flag");
        int flag = Integer.parseInt(setting.getValue());
        GloryUser user = userService.selectById(userId);
        user.setFlag(user.getFlag() + flag);
        userService.updateById(user);
        JSONObject result = new JSONObject();
        result.put("shareRecord", shareRecord);
        result.put("shareFlag", setting);
        return new SuccessTip(result);
    }
}
